<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
 <?php
 	$user_name=$_POST['user_name'];
	$user_psw=$_POST['user_psw'];
	if($user_name!=""&&$user_psw!=""){
		$conn=mysql_connect("localhost","root","root") or die("连接数据库失败！");
		mysql_select_db("guestbook",$conn) or die("打开数据库失败！");
		$result=mysql_query("select *from userinfo where username='$user_name'");
		$row=mysql_fetch_array($result);
		if($row){			
			echo "<script>alert('该用户已存在！');
			window.location.href='../view/register.php'; 
			</script>";
		}else{
			$insert=mysql_query("insert into userinfo values(null,'$user_name','$user_psw');");
			if($insert){
				echo "<script>alert('注册成功，请登录！');
				window.location.href='../index.php'; 
				</script>";
			}
			else{
				echo "<script>alert('注册失败！');
				window.location.href='../view/register.php'; 
				</script>";
			}
		}
		mysql_close($conn);
	}else{
		echo "<script>alert('用户名或密码为空！');
		window.location.href='../view/register.php'; 
		</script>";
	}
	
 ?>
</body>
</html>